import pathToRegexp from "path-to-regexp";
import dayjs from "dayjs";
import { Vue } from "vue/types/vue";
import { isNil, isNaN, isNumber } from "lodash";

export const getEnums: any = (key: any) => {
  const o = require("@/locale/lang/zh-CN").default;
  return Object.keys(o[key] || {}).map((item) => ({
    value: item,
    label: o[key][item]
  }));
};

/**
 * 列表按钮权限
 * @param {object} btnMap 权限清单
 * @param {string} btnKey 按钮key
 * @param {string} value 判定值
 *
 * @example
 *
 * btnMap: {edit_plan: {name: "修改生产计划", key: "edit_plan", status: ["INVALID"]}, delete_plan: {name: "删除生产计划", key: "delete_plan", status: ["INVALID"]}}
 * btnKey: edit_plan
 * value: 'INVALID'
 * return true
 */
export function btnPermission(btnMap: any, btnKey: any, value: any) {
  return btnMap[btnKey] ? btnMap[btnKey].status.includes(value) : false;
}

/**
 * // 利用车间班组对象数组，取出班组和班组负责人名称，转成字符串放在一个对象里return出去
 * @param {object[]} arr 车间班组
 */
export function workshopFormat(array: object[]) {
  const array1 = array.map((item: any) => {
    return item.leader_obj.name;
  });
  const array2 = array.map((item: any) => {
    return item.name;
  });
  const obj = {
    leader_obj_name: array1.join(","),
    workshop_group_name: array2.join(",")
  };
  return obj;
}

export const resfilter = (options: any) => {
  const format = options.format.split(".");
  let index = 0;
  return function fn(res: any): any {
    if (format[index]) {
      const newRes = res[format[index]];
      index++;
      return fn(newRes);
    } else {
      index = 0;
      return res;
    }
  };
};
/**
 * 格式化金额
 * @param  {[type]} v     [要转换的数字]
 * @param  {[type]} len   [小数点位数,默认2位]
 * @param  {[type]} split [分隔符,默认',']
 * @return {[type]}       [返回转换完的字符串]
 */
export function fmoney(v: number | string, len: number, split = ",") {
  if (!isNumber(v)) { return ""; }
  len = Math.abs(+len % 20 || 2);
  v = parseFloat((v + "").replace(/[^\d .-]/g, "")).toFixed(len) + "";
  return v.replace(/\d+/, (rv: string) => {
    const lit = rv.length % 3 === 0;
    const index = lit ? rv.length - 3 : -1;
    return rv
      .split("")
      .reverse()
      .join("")
      .replace(/\d{3}/g, (k: any, l: any) => {
        return k + (l === index && lit ? "" : split);
      })
      .split("")
      .reverse()
      .join("");
  });
}

/**
 * 如果为空、null、undefined 则用---占位
 * @param val 参数
 */
export function placeholder(val: any) {
  if (isNil(val) || isNaN(val) || val === "") {
    return "-";
  }
  return val;
}
